package com.accesshollywood.notifications;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.EnumSet;

import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.indexables.GrfxOrder;
import com.accesshollywood.indexables.User;
import com.accesshollywood.prefs.Prefs;

public class Notify {
	public static void orderAssigned(GrfxOrder currOrder, User assignee, User assigner) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.orderAssigned(currOrder, assignee, assigner);
			}
			if (Users.currentUser == assignee) {
				if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
					SpokenAlert.orderAssigned(currOrder, assignee, assigner);
				}
				if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
					ChimeAlert.orderAssigned();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void orderUnAssigned(GrfxOrder currOrder, User assignee, User assigner) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.orderUnAssigned(currOrder, assignee, assigner);
			}
			if (Users.currentUser == assignee) {
				if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
					SpokenAlert.orderUnAssigned(currOrder, assignee, assigner);
				}
				if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
					ChimeAlert.orderUnAssigned();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void orderModified(GrfxOrder oldOrder, GrfxOrder currOrder, User changer) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.orderModified(oldOrder, currOrder, changer);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.orderModified(oldOrder, currOrder, changer);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.orderModified();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void textEdited(GrfxOrder currOrder, User changer, String oldText, String newText) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.textEdited(currOrder, changer, oldText, newText);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.textEdited(currOrder, changer, oldText, newText);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.textEdited();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void photosChanged(GrfxOrder currOrder, User changer, String oldPhotos, String newPhotos) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.photosChanged(currOrder, changer, oldPhotos, newPhotos);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.photosChanged(currOrder, changer, oldPhotos, newPhotos);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.photosChanged();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void footageChanged(GrfxOrder currOrder, User changer, String oldFootage, String newFootage) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.footageChanged(currOrder, changer, oldFootage, newFootage);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.footageChanged(currOrder, changer, oldFootage, newFootage);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.footageChanged();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void deadlineChanged(GrfxOrder currOrder, User changer, String oldDeadline, String newDeadline) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.deadlineChanged(currOrder, changer, oldDeadline, newDeadline);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.deadlineChanged(currOrder, changer, oldDeadline, newDeadline);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.deadlineChanged();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void deadlineApproaching(GrfxOrder currOrder) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.deadlineApproaching(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.deadlineApproaching(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.deadlineApproaching();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void notesChanged(GrfxOrder currOrder, User changer, String oldNotes, String newNotes) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.notesChanged(currOrder, changer, oldNotes, newNotes);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.notesChanged(currOrder, changer, oldNotes, newNotes);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.notesChanged();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void researchApproved(GrfxOrder currOrder) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.researchApproved(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.researchApproved(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.researchApproved();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void photoEditApproved(GrfxOrder currOrder) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.photoEditApproved(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.photoEditApproved(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.photoEditApproved();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void footageApproved(GrfxOrder currOrder) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.footageApproved(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.footageApproved(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.footageApproved();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void legalApproved(GrfxOrder currOrder) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.legalApproved(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.legalApproved(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.legalApproved();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void markedComplete(GrfxOrder currOrder) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.markedComplete(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.markedComplete(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.markedComplete();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void orderClosed(GrfxOrder currOrder) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.orderClosed(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.orderClosed(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.orderClosed();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void orderReopened(GrfxOrder currOrder, User reOpener) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.orderReopened(currOrder, reOpener);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.orderReopened(currOrder, reOpener);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.orderReopened();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void orderArchived(GrfxOrder currOrder) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.orderArchived(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.orderArchived(currOrder);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.orderClosed();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void orderUnArchived(GrfxOrder currOrder, User currentUser) {
		try {
			StackTraceElement[] ste = Thread.currentThread().getStackTrace();
			String methodName = ste[1].getMethodName();

			Field currField = Prefs.Notifications.class.getField(methodName);
			EnumSet currPref = (EnumSet) currField.get(currField);

			if (currPref.contains(Prefs.Notifications.NotifyType.EMAIL)) {
				EmailAlert.orderUnArchived(currOrder, currentUser);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.SPEAK)) {
				SpokenAlert.orderUnArchived(currOrder, currentUser);
			}
			if (currPref.contains(Prefs.Notifications.NotifyType.CHIME)) {
				ChimeAlert.orderReopened();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
